package net.mysoft.budgetsoft.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.mysoft.budgetsoft.system.dao.MenuDao;
import net.mysoft.budgetsoft.system.domain.PageData;
import net.mysoft.budgetsoft.system.domain.TMenu;
import net.mysoft.budgetsoft.system.service.MenuService;
import net.mysoft.framework.util.Lists;
import net.mysoft.framework.util.Maps;
import net.mysoft.framework.util.Tool;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@SuppressWarnings({"rawtypes", "unchecked"})
@Service
public class MenuSeriviceImpl implements MenuService {
	
	@Autowired
	private MenuDao menuDao;

	@Override
	public List getMenuTree() {
		List<TMenu> menus = menuDao.find("from TMenu");
		List tree = new ArrayList();
		for(TMenu menu:menus) {
			Map map = Maps.newHashMap("id", menu.getId(), "text", menu.getName());
			if(menu.getParent()!=null) map.put("pid", menu.getParent().getId());
			tree.add(map);
		}
		return tree;
	}

	@Override
	public PageData getMenuDatagrid(Map map) {
		String pid = Tool.toString(map.get("pid"));
		int pageNo = Tool.toInt(map.get("page"));
		pageNo = pageNo==0?1:pageNo;
		boolean flag = StringUtils.isBlank(pid);
		List<TMenu> menus = menuDao.queryPageByHql("select new map(id as id, name as name, "+
				"code as code, url as url) from TMenu "	+(flag?"":"where parent.id = :pid"), 
				flag?null:Maps.newHashMap("pid", pid), pageNo, 10);
		int count = menuDao.count("select count(*) from TMenu " + (flag?"":"where parent.id = ?"), 
				flag?null:Lists.newArrayList(pid)).intValue();
		PageData<TMenu> page = new PageData<TMenu>();
		page.setPageNumber(pageNo);
		page.setPageSize(10);
		page.setRows(menus);
		page.setTotal(count);
		return page;
	}

	@Override
	public void addMenu(Map map) {
		TMenu parent = menuDao.load(TMenu.class, Tool.toString(map.get("pid")));
		TMenu root = menuDao.load(TMenu.class, Tool.toString(map.get("rid")));
		TMenu menu = new TMenu();
		menu.setName(Tool.toString(map.get("name")));
		menu.setCode(Tool.toString(map.get("code")));
		menu.setUrl(Tool.toString(map.get("url")));
		//menu.setCreateDate(new Date());
		if(parent!=null) menu.setParent(parent);
		if(root!=null) menu.setRoot(root);
		menuDao.save(menu);
	}

	@Override
	public TMenu getMenu(String id) {
		return menuDao.load(TMenu.class, id);
	}

	@Override
	public void updateMenu(Map map) {
		TMenu menu = menuDao.load(TMenu.class, Tool.toString(map.get("id")));
		menu.setName(Tool.toString(map.get("name")));
		menu.setCode(Tool.toString(map.get("code")));
		menu.setUrl(Tool.toString(map.get("url")));
		menuDao.save(menu);
	}

	@Override
	public void delMenus(String ids) {
		if(StringUtils.isNotBlank(ids)) {
			String[] idz = ids.split(",");
			for(String id:idz) {
				if(StringUtils.isNotBlank(id)) {
					menuDao.delete(menuDao.load(TMenu.class, id));
				}
			}
		}
	}

}
